package entities;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public abstract class ReadXMLFile {
	   protected static final String ns = null;
	   
	   protected static String tagName;

	   protected static String branchName;
		
	   protected static String[] fieldNames;
	   
	   
	   
	   /**
	    * Initilize tagName, branchName, fieldNames
	    */
	   protected abstract void init(InputStream in);
	   	   
	   protected List<String> readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
	        parser.require(XmlPullParser.START_TAG, ns, tagName);
            List<String> fields = new ArrayList<String>();
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String tag = parser.getName();     

	            for(String s: fieldNames){
	            	if(tag.equals(s)){
	            		fields.add(readField(parser, s));
	            	}            	
	            }
	        }
	      return fields;
	    }

	    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
	        String result = "";
	        if (parser.next() == XmlPullParser.TEXT) {
	            result = parser.getText();
	            parser.nextTag();
	        }
	        return result;
	    }
	    
	    // Processes title tags in the feed.
	    protected String readField(XmlPullParser parser, String fieldName) throws IOException, XmlPullParserException {
	        parser.require(XmlPullParser.START_TAG, ns, fieldName);
	        String title = readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, fieldName);
	        return title;
	    }
	      
	  
	    
	    protected List<List<?>> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
	        List<List<?>> entries = new ArrayList<List<?>>();
	        
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            // Starts by looking for the entry tag
	            if (name.equals(tagName)) {
	            	entries.add(readEntry(parser));
	            }
	        }  
	        return entries;
	    }
	    
	    @SuppressWarnings("rawtypes")
		protected List parse(InputStream in) throws XmlPullParserException, IOException {
	        try {
	            XmlPullParser parser = Xml.newPullParser();
	            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
	            parser.setInput(in, null);
	            parser.nextTag();
	            return readFeed(parser);
	        } finally {
	            in.close();
	        }
	    }

	    
}
